package com.example.livingplace;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONHelper {

        private static int id = 0;
        private static final String ID_TEXT = "christian_philip";
        private static final String SERVER = "172.16.0.200";
        private static final String PORT = "12349";

        public static String[] createMessage(String topic, String action,
                        Map<String, String> valueMap) {
                Map<String, Object> changeMap = new HashMap<String, Object>();
                changeMap.put("values", new JSONObject(valueMap));
                changeMap.put("Id", ID_TEXT + id++);
                changeMap.put("Version", null);
                changeMap.put("action", action);
                JSONObject msg = new JSONObject(changeMap);

                String[] params = new String[5];
                params[0] = SERVER;
                params[1] = PORT;
                params[2] = topic;
                params[3] = "topic";
                params[4] = msg.toString();
                Log.d(JSONHelper.class.getSimpleName(), msg.toString());

                return params;
        }

        public static String[] createWindowMessage(String topic, String message) {
                JSONObject msg;
                try {
                        msg = new JSONObject(message);

                        String[] params = new String[5];
                        params[0] = SERVER;
                        params[1] = PORT;
                        params[2] = topic;
                        params[3] = "topic";
                        params[4] = msg.toString();
                        Log.d(JSONHelper.class.getSimpleName(), msg.toString());

                        return params;

                } catch (JSONException e) {
                        e.printStackTrace();
                }

                return null;
        }
        
        public static String[] createHeatingMessage(String topic, String module, String hexValue) {
                Map<String, Object> changeMap = new HashMap<String, Object>();
                changeMap.put(module, hexValue);
                changeMap.put("Id", ID_TEXT + id++);
                changeMap.put("Version", null);
                changeMap.put("action", "");
                JSONObject msg = new JSONObject(changeMap);

                String[] params = new String[5];
                params[0] = SERVER;
                params[1] = PORT;
                params[2] = topic;
                params[3] = "topic";
                params[4] = msg.toString();
                Log.d(JSONHelper.class.getSimpleName(), msg.toString());

                return params;
        }

}